package br.com.futibamanager.bean;

import javax.faces.event.ActionEvent;

import br.com.futibamanager.dao.UserDAO;
import br.com.futibamanager.model.User;
import br.com.futibamanager.util.Constants;
import br.com.futibamanager.util.EmailSender;
import br.com.futibamanager.util.FacesUtil;

/**
 * The User Managed Bean.
 * 
 * @author diogenes.feijo
 *
 */
public class UserMB {

	private User user;
	
	private UserDAO userDAO = new UserDAO();
	
	/**
	 * Default Constructor (resets MB's state)
	 */
	public UserMB() {
		
		reset();
	}
	
	/**
	 * Saves the member.
	 * 
	 * @return String
	 */
	public String saveMember() {

		if (isLoginAlreadyRegistered()) {
			
			FacesUtil.addErrorMessage("login.already.registered");
			
		} else if (isEmailAlreadyRegistered()) {
			
			FacesUtil.addErrorMessage("email.already.registered");
			
		} else {
			
			user.setType(Constants.MEMBER_USER_TYPE_ID);
			
			user.setLoggedIn(true);
			
			userDAO.save(user);
			
			EmailSender.sendRegisteredUserEmail(user);
			
			FacesUtil.addInfoMessage("user.registered.successfully");
			
			return "main";
		}

		return "registerUser";
	}
	
	/**
	 * Makes the login.
	 * 
	 * @param actionEvent - the ActionEvent
	 */
	public void login(ActionEvent actionEvent) {
		
		User foundUser = 
			userDAO.findByLoginAndPassword(user.getLogin(), user.getPassword());
		
		if (foundUser != null) {
			
			setUser(foundUser);
			
			user.setLoggedIn(true);
			
			FacesUtil.navigate("/pages/main.jsf", false);
			
		} else {
			
			FacesUtil.addErrorMessage("user.not.registered");
			
			reset();
		}
	}
	
	/**
	 * Makes the logout.
	 * 
	 * @return String
	 */
	public String logout() {
		
		reset();
		
		return "main";
	}
	
	/**
	 * Recover user's data by e-mail.
	 * 
	 * @return String
	 */
	public String recoverData() {
		
		User foundUser = userDAO.findByEmail(user.getEmail());
		
		if (foundUser == null) {
			FacesUtil.addErrorMessage("user.email.not.registered");
			
			return null;
		}
		
		FacesUtil.addInfoMessage("user.data.sent.by.email.successfully");
		
		EmailSender.sendUserDataEmail(foundUser);
		
		return "main";
	}
	
	/**
	 * Loads the register page.
	 * 
	 * @return String
	 */
	public String loadRegisterPage() {
		
		reset();

		return "registerUser";
	}
	
	/**
	 * Loads the recover data page.
	 * 
	 * @return String
	 */
	public String loadRecoverDataPage() {
		
		reset();

		return "recoverUserData";
	}
	
	/*
	 * Verifies if the user's login is already registered.
	 */
	private boolean isLoginAlreadyRegistered() {

		return !userDAO.findByLogin(user.getLogin()).isEmpty();
	}
	
	/*
	 * Verifies if the user's e-mail is already registered.
	 */
	private boolean isEmailAlreadyRegistered() {

		return userDAO.findByEmail(user.getEmail()) != null;
	}
	
	/*
	 * Resets MB's state
	 */
	private void reset() {
		
		user = new User();
	}

	/**
	 * Gets the user.
	 * 
	 * @return User
	 */
	public User getUser() {
		return user;
	}

	/**
	 * Sets the user.
	 * 
	 * @param user - the user
	 */
	public void setUser(User user) {
		this.user = user;
	}
	
}